package com.xinghou.platform.dao.mange.control;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xinghou.platform.dao.mange.service.AttachmentDAOService;
import com.xinghou.platform.dao.mange.service.LoadResourceService;
import com.xinghou.platform.dao.mange.service.MessageDAOService;
import com.xinghou.platform.dao.mange.service.PhotoDAOService;
import com.xinghou.platform.dao.mange.service.UserDAOService;
import com.xinghou.platform.dao.mange.service.UserInfoDAOService;
import com.xinghou.platform.dao.mange.service.VestAssignedDAOService;
import com.xinghou.platform.model.main.Message;
import com.xinghou.platform.model.main.VestAssigned;

@Controller
@RequestMapping("/index") 
public class mainActionControl {
	@Resource
	private MessageDAOService messageDAOService;
	
	@Resource
	private UserInfoDAOService userinfoDAOService;
	
	@Resource
	private VestAssignedDAOService vestAssignedDAOService;

	@Resource
	private LoadResourceService loadResourceService;
	
	@Resource
	private AttachmentDAOService attachmentDAOService;
	
	@Resource
	private PhotoDAOService photoDAOService;
	
	@Resource
	private UserDAOService userDAOService;
	
	
	@RequestMapping(value = "/index.action")
	public  String jsonindex(HttpServletRequest request,HttpServletResponse response){
	     	List<VestAssigned> vestAssignedlists=new ArrayList<VestAssigned>();
               int vestId=Integer.valueOf(request.getParameter("vestId"));
                List<VestAssigned> vestAssignedlist=vestAssignedDAOService.getvestAssigned(vestId);
                for(VestAssigned vest:vestAssignedlist)
                {
                	List<String> userInfoid=userDAOService.getInfoIdByUseId(vest.getFakeId());
                	if(userInfoid.size()>0)
                	{
                		List<String> heardid=userinfoDAOService.getHeadIdByInfoId(Integer.valueOf(userInfoid.get(0)));
                	
                	vest.setReceiverphotoname("");
                	vest.setReceiverfileUri("");
                   if(photoDAOService.photo(Integer.valueOf(heardid.get(0)))!=null)
      			   {
     				    vest.setReceiverphotoname(photoDAOService.photo(Integer.valueOf(heardid.get(0))).getName());
                        vest.setReceiverfileUri(photoDAOService.photo(Integer.valueOf(heardid.get(0))).getPhotoUri());
                        vestAssignedlists.add(vest);
    			   }
                	}
                }


			    JSONArray json=JSONArray.fromObject(vestAssignedlists);
		       // 设置编码
		        response.setCharacterEncoding("UTF-8");
		       // 写入到前台
		        try {
					response.getWriter().write(json.toString());
				   
				} catch (IOException e) {
					
					e.printStackTrace();
				}
          return null;
	}
	
	@RequestMapping(value = "/talk.action")
	public  String talk(HttpServletRequest request,HttpServletResponse response){
		   
          return "talk";
	}
	
	@RequestMapping(value = "/save.action")
	public  String save(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
       
        Message message=new Message();
        message.setReceiverId(Integer.valueOf(request.getParameter("receiverId")));
        message.setSenderId(Integer.valueOf(request.getParameter("senderId")));
        message.setMsgType(Short.parseShort(request.getParameter("msgType")));
        message.setOrigin(Short.parseShort(request.getParameter("origin")));
        message.setText("");
        if(request.getParameter("text")!=null)
        {
        	message.setText(new String(request.getParameter("text").getBytes("ISO-8859-1"),"UTF-8"));
        }
        	
        message.setStatus(Short.parseShort(request.getParameter("status")));
        int msg=1;
        try {
		     messageDAOService.saveMsg(message);
		  
		} catch (DataAccessException e) {
			msg=0;
			e.printStackTrace();
		}
        JSONArray json=JSONArray.fromObject(msg);
        try {
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
       return null;
	}
	
	@RequestMapping(value = "/close.action")
	public  String close(HttpServletRequest request,HttpServletResponse response){
		   int id=Integer.valueOf(request.getParameter("Id"));
		   int msgId=Integer.valueOf(request.getParameter("msgId"));
		   try {
			   vestAssignedDAOService.delVestAssigned(id);
			   messageDAOService.updateMessageBymsgid(msgId);
			   loadResourceService.start();	   
		} catch (DataAccessException e) {
			
			e.printStackTrace();
		}
		 
		   int vestId=Integer.valueOf(request.getParameter("vestId"));
           List<VestAssigned> vestAssignedlist=vestAssignedDAOService.getvestAssigned(vestId);
		    JSONArray json=JSONArray.fromObject(vestAssignedlist);
	       // 设置编码
	        response.setCharacterEncoding("UTF-8");
	       // 写入到前台
	        try {
				response.getWriter().write(json.toString());
			   
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		  
		   return null;
	}
	
	@RequestMapping(value = "/chaxun.action")
	public  String chaxun(HttpServletRequest request,HttpServletResponse response){
		  
		   int senderId=Integer.valueOf(request.getParameter("senderId"));
		   int receiverId=Integer.valueOf(request.getParameter("receiverId"));
		   List<Message> messageList=messageDAOService.getMsgss(senderId, receiverId,(short)1);
		   List<Message> messageLists=new ArrayList<Message>();
		 for(Message message:messageList)
		 {
			 
			 message.setText(message.getText());
			 message.setSenderName(userinfoDAOService.getUserInfoById(message.getSenderId()).getNickName());
			 message.setReceiverName(userinfoDAOService.getUserInfoById(message.getReceiverId()).getNickName());
			 
			 List<String> fileUri =attachmentDAOService.getAttachmentById(message.getAttachId());
			 message.setFileUri("");
			   if(fileUri.size()>0)
			   {
				   message.setFileUri(fileUri.get(0)); 
			   }
			  messageLists.add(message);
		 }
		   JSONArray json=JSONArray.fromObject(messageLists);
	       // 设置编码
	        response.setCharacterEncoding("UTF-8");
	       // 写入到前台
	        try {
				response.getWriter().write(json.toString());
			   
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		   return null;
	}
	
	
	@RequestMapping(value = "/timeselect.action")
	public  String timeselect(HttpServletRequest request,HttpServletResponse response){
		  List<Message> messageLists=new ArrayList<Message>();
		   int senderId=Integer.valueOf(request.getParameter("timesenderId"));
		   int receiverId=Integer.valueOf(request.getParameter("timereceiverId"));
		   List<Message> messageList=messageDAOService.getMsgss(senderId, receiverId);
		   for(Message message:messageList)
		   {
           	List<String> userInfoid=userDAOService.getInfoIdByUseId(senderId);
              if(userInfoid.size()>0)
              {
          		List<String> heardid=userinfoDAOService.getHeadIdByInfoId(Integer.valueOf(userInfoid.get(0)));

             
			   message.setSenderphotoname("");   
			   message.setSenderfileUri("");
			   if(photoDAOService.photo(Integer.valueOf(heardid.get(0)))!=null)
			   {
				   
				   message.setSenderphotoname(photoDAOService.photo(Integer.valueOf(heardid.get(0))).getName());
				   message.setSenderfileUri(photoDAOService.photo(Integer.valueOf(heardid.get(0))).getPhotoUri());

			   }
              }  
			   messageDAOService.updateMessageBymsgid(message.getMsgId());
			   List<String> fileUri =attachmentDAOService.getAttachmentById(message.getAttachId());
			   message.setFileUri("");
			   if(fileUri.size()>0)
			   {
				   message.setFileUri(fileUri.get(0)); 
			   }
			   
			   messageLists.add(message);
		   }
		 
		   JSONArray json=JSONArray.fromObject(messageLists);
	       // 设置编码
	        response.setCharacterEncoding("UTF-8");
	       // 写入到前台
	        try {
				response.getWriter().write(json.toString());
			   
			} catch (IOException e) {
				
				e.printStackTrace();
			}
	      return null;
	}
	
	
	@RequestMapping(value = "/timeshect.action")
	public  String timeshect(HttpServletRequest request,HttpServletResponse response){
		  
		   int senderId=Integer.valueOf(request.getParameter("timesenderId"));
		   int receiverId=Integer.valueOf(request.getParameter("timereceiverId"));
		   List<Message> messageList=messageDAOService.getMsgss(senderId, receiverId);
		   JSONArray json=JSONArray.fromObject(messageList);
	       // 设置编码
	        response.setCharacterEncoding("UTF-8");
	       // 写入到前台
	        try {
				response.getWriter().write(json.toString());
			   
			} catch (IOException e) {
				
				e.printStackTrace();
			}
	      return null;
	}
}
